
package com.lt.test.provider.dao.impl;

import com.lt.test.provider.dao.BaseDaoSupport;
import com.lt.test.provider.dao.KtPurchaseOrderItemDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class KtPurchaseOrderItemDaoImpl extends BaseDaoSupport implements KtPurchaseOrderItemDao {

	private static final Logger log = Logger.getLogger(KtPurchaseOrderItemDaoImpl.class);

	/**
	 * 根据查询条件获取实现集
	 */
	public List query(Map parameter) {
		List list = new ArrayList();
		try {
			list = this.queryForList("queryKtPurchaseOrderItems",parameter);
		} catch (Exception e) {
			log.error("错误:");
			e.printStackTrace();
		}
	    return list;
	}


	/**
	 * 创建实体
	 */
    public int create(Map parameter)
    {
    	return this.insert("createKtPurchaseOrderItem", parameter);
    }

    /**
     * 更新实体
     */
    public int update(Map parameter)
    {
    	return this.update("updateKtPurchaseOrderItem",parameter);
    }

    /**
     * 保存实体
     */
    public int save(Map parameter)
    {
    	return this.update("saveKtPurchaseOrderItem",parameter);
    }


     /**
     * 删除实体
     */
    public int delete(java.lang.String id)
    {
    	return this.delete("deleteKtPurchaseOrderItem",id);
    }
    /**
	 * 根据主键获取实体信息
	 */
	public Map findById(java.lang.String id) {
		return this.queryForSingle("queryKtPurchaseOrderItem", id);
	}

  	/**
     * 批量插入
     */
	public int batch(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.insert("batchKtPurchaseOrderItem",values);
  		return iEffectedRow;
	}

  	/**
     * 批量更新
     */
  	public int batchUpdate(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.update("batchUpdateKtPurchaseOrderItem",values);
  		return iEffectedRow;
	}

  	/**
     * 批量删除
     */
  	public int batchDelete(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.delete("batchDeleteKtPurchaseOrderItem",values);
  		return iEffectedRow;
	}

  	@Override
  	public void deletePurchaseOrderItem(String purchaseOrderId) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("DELETE FROM KT_PURCHASE_ORDER_ITEM WHERE PURCHASE_ID = ? ");
  		this.excuteSql(sqlStr.toString(), new Object[]{purchaseOrderId});
  	}

  	@Override
  	public List<Map<String, Object>> getPurchaseOrderItems(String id) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append(" SELECT t2.id purchase_order_item_id,t2.purchase_id,t1.SELF_CODE,T1.PRODUCT_CODE,t1.PRODUCT_NAME,t1.UNIT,T1.SPEC,T1.STOCK_SPEC,T2.QUANTITY,T2.CST_PRICE,T2.AMT,T2.REMARK,T2.SUPPLIER_ID,T3.SUPPLIER_NAME,T2.REAL_QUANTITY,T2.SHOP_ID,T4.SHOP_NAME,t2.type FROM D_PROD t1,KT_PURCHASE_ORDER_ITEM t2 LEFT JOIN KT_SUPPLIER t3 ON T2.SUPPLIER_ID =t3.ID LEFT JOIN D_SHOP t4 ON t2.SHOP_ID = T4.SHOP_ID WHERE T1.PRODUCT_CODE = T2.PRODUCT_CODE AND T2.PURCHASE_ID = ?  ");
  		return this.queryAsArray(sqlStr.toString(), new Object[]{id});
  	}

  	@Override
  	public void rewriteRealquantity(String id) {
		System.out.println(id);
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append(" MERGE INTO KT_PURCHASE_ORDER_ITEM T1 ");
		sqlStr.append(" USING(SELECT T1.BATCH_NO,T2.PRODUCT_CODE,SUM(T2.REAL_QUANTITY) REAL_QUANTITY FROM KT_SHOP_INSTOCK_ORDER t1 , KT_SHOP_INSTOCK_ORDER_ITEM t2 WHERE T1.id = T2.INSTOCK_ID AND T1.ID = ? GROUP BY BATCH_NO,PRODUCT_CODE ) T2  ");
		sqlStr.append(" ON(T1.PURCHASE_ID =T2.BATCH_NO AND T1.PRODUCT_CODE = T2.PRODUCT_CODE ) ");
		sqlStr.append("  WHEN MATCHED THEN update set T1.REAL_QUANTITY = T2.REAL_QUANTITY ");
		this.excuteSql(sqlStr.toString(), new Object[]{id});
  	}

  	@Override
  	public void rewriteLossProfitRealquantity(String id) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append(" MERGE INTO KT_SHOP_LOSSPROFIT_ITEM T1 ");
		sqlStr.append(" USING(SELECT T1.BATCH_NO,T2.PRODUCT_CODE,SUM(T2.REAL_QUANTITY) REAL_QUANTITY FROM KT_SHOP_INSTOCK_ORDER t1 , KT_SHOP_INSTOCK_ORDER_ITEM t2 WHERE T1.id = T2.INSTOCK_ID AND T1.ID = ? GROUP BY BATCH_NO,PRODUCT_CODE ) T2  ");
		sqlStr.append(" ON(T1.ORDER_ID =T2.BATCH_NO AND T1.PRODUCT_CODE = T2.PRODUCT_CODE ) ");
		sqlStr.append("  WHEN MATCHED THEN update set T1.REAL_QUANTITY = T2.REAL_QUANTITY ");
		this.excuteSql(sqlStr.toString(), new Object[]{id});
  	}

  	@Override
  	public void rewriteRealOutstockQuantity(String id) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append(" MERGE INTO KT_PURCHASE_ORDER_ITEM T1 ");
		sqlStr.append(" USING(SELECT T1.BATCH_NO,T2.PRODUCT_CODE,SUM(T2.REAL_QUANTITY) REAL_QUANTITY FROM KT_SHOP_OUTSTOCK_ORDER t1 , KT_SHOP_OUTSTOCK_ORDER_ITEM t2 WHERE T1.id = T2.OUTSTOCK_ID AND T1.ID = ? GROUP BY BATCH_NO,PRODUCT_CODE  ) T2  ");
		sqlStr.append(" ON(T1.PURCHASE_ID =T2.BATCH_NO AND T1.PRODUCT_CODE = T2.PRODUCT_CODE ) ");
		sqlStr.append("  WHEN MATCHED THEN update set T1.REAL_QUANTITY = T2.REAL_QUANTITY ");
		this.excuteSql(sqlStr.toString(), new Object[]{id});
  	}

  	@Override
  	public void updateOrderItemApproveFlag(String purchaseOrderId, String approveFlag) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("UPDATE KT_PURCHASE_ORDER_ITEM SET APPROVE_FLAG = ? WHERE PURCHASE_ID = ?");
  		this.excuteSql(sqlStr.toString(), new Object[]{purchaseOrderId,approveFlag});
  	}


  	@Override
  	public List<Map<String, Object>> getUnFinishPurchaseOrderItems(String purchaseId) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append(" SELECT 1 from KT_PURCHASE_ORDER_ITEM WHERE PURCHASE_ID = ? AND TYPE ='5' ");
  		return this.queryAsArray(sqlStr.toString(), new Object[]{purchaseId});
  	}
}
